Under the hood
@ryfylke-react/toast is built using CustomEvent.
When you initialize the library using initToast, these things happen:
- We generate a random event-type with a hardcoded prefix.
- We configure
toast,useToasts,ToastProviderandsubscribeToToaststo post to and listen to this event-type (or "channel"). - We apply the generic types supplied by the user to the utils.
- We return the utils to the user.
useToastsutilizessubscribeToToastsinternally with auseEffectand some state.ToastProviderutilizesuseToastsinternally, and exposes some props to help the user render the toast-list.
Really it all boils down subscribing to CustomEvents and exposing some helpers to the user. useToasts is just some sugar on top of subscribeToToasts, and ToastProvider is just some sugar on top of useToasts.